package com.imoblife.brainwavestus.oldMedia;

import com.imoblife.brainwavestus.R;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class SetVolumDialog {

	Button okButton;

	Button cancalButton;

	SeekBar seekBar;
	Activity context;
	Dialog dialog;

	public SetVolumDialog(Activity in) {
		context = in;
		initAction();
	}

	public void initAction() {
		Button button = (Button) context.findViewById(R.id.home_volume_button);
		button.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				showDialog();
			}
		});
		initSystemVolum();
	}

	private void showDialog() {
		dialog = new Dialog(context, android.R.style.Theme_Light_Panel);

		dialog.setContentView(R.layout.volum_layout);

		dialog.show();

		okButton = (Button) dialog.findViewById(R.id.ok);
		cancalButton = (Button) dialog.findViewById(R.id.cancel);
		seekBar = (SeekBar) dialog.findViewById(R.id.play_progress);
		initSeekBar();
		initCancal();
	}

	int size;

	private void initSystemVolum() {
		AudioManager audioManager = (AudioManager) context
				.getSystemService(Context.AUDIO_SERVICE);
		int maxSize = audioManager
				.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		SharedPreferences preferences = context.getSharedPreferences("Volume",
				0);

		int size = preferences.getInt("VolumeSize", (int) (maxSize * 0.5));
		audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, size, 0);

	}

	AudioManager audioManager;
	SharedPreferences preferences;

	void initSeekBar() {

		audioManager = (AudioManager) context
				.getSystemService(Context.AUDIO_SERVICE);
		final int maxSize = audioManager
				.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		preferences = context.getSharedPreferences("Volume", 0);

		size = preferences.getInt("VolumeSize", (int) (maxSize * 0.5));
		audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, size, 0);

		seekBar.setMax(maxSize);
		seekBar.setProgress(size);

		seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			public void onStopTrackingTouch(SeekBar seekBar) {
				

			}

			public void onStartTrackingTouch(SeekBar seekBar) {
				

			}

			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				
				audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
						progress, 0);

			}
		});
	}

	private void initCancal() {

		okButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				size = seekBar.getProgress();
				dialog.cancel();
			}
		});

		cancalButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				dialog.cancel();

			}
		});

		dialog.setOnCancelListener(new OnCancelListener() {

			public void onCancel(DialogInterface dialog) {
				audioManager
						.setStreamVolume(AudioManager.STREAM_MUSIC, size, 0);
				preferences.edit().putInt("VolumeSize", size).commit();
			}
		});

	}
}
